package com.yzy.fly.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.yzy.fly.config.properties.DruidProperties;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

//@Configuration
//@MapperScan("com.yzy.coredataapi.mapper.vhlmodel*")
@Deprecated
public class MybatisPlusConfig {
    @Autowired
    private DruidProperties druidProperties;
   /*
    * oracle数据库配置JdbcTypeForNull
    * 参考：https://gitee.com/baomidou/mybatisplus-boot-starter/issues/IHS8X
    不需要这样配置了，参考 yml:
    mybatis-plus:
      confuguration
        dbc-type-for-null: 'null'
   @Bean
   public ConfigurationCustomizer configurationCustomizer(){
       return new MybatisPlusCustomizers();
   }

   class MybatisPlusCustomizers implements ConfigurationCustomizer {

       @Override
       public void customize(org.apache.ibatis.session.Configuration configuration) {
           configuration.setJdbcTypeForNull(JdbcType.NULL);
       }
   }
   */
   @Bean(name = "vhlcore")
   @ConfigurationProperties(prefix = "spring.datasource")
   public DataSource vhlcore() {
       DruidDataSource build = DruidDataSourceBuilder.create().build();
       // 配置一个连接在池中最小生存的时间，单位是毫秒
       build.setMinEvictableIdleTimeMillis(druidProperties.getMinEvictableIdleTimeMillis());
       build.setMaxEvictableIdleTimeMillis(druidProperties.getMaxEvictableIdleTimeMillis());
       return build;
   }
    @Bean(name = "vhlmodellibrary")
    @ConfigurationProperties(prefix = "spring.datasource.druid.vhlmodellibrary")
    public DataSource vhlmodellibrary() {
        return DruidDataSourceBuilder.create().build();
    }
    /**
     * 动态数据源配置
     *
     * @return
     */
    @Bean
    @Primary
    public DataSource multipleDataSource(@Qualifier("vhlmodellibrary") DataSource vhlmodellibrary,
                                         @Qualifier("vhlcore") DataSource vhlcore) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DBTypeEnum.vhlmodellibrary.getValue(), vhlmodellibrary);
        targetDataSources.put(DBTypeEnum.vhlcore.getValue(), vhlcore);
//        targetDataSources.put(DBTypeEnum.db3.getValue(), db3);
        dynamicDataSource.setTargetDataSources(targetDataSources);
        dynamicDataSource.setDefaultTargetDataSource(vhlcore);
        return dynamicDataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(multipleDataSource(vhlmodellibrary(),vhlcore()));

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(false);
        configuration.setCacheEnabled(false);
        
        sqlSessionFactory.setConfiguration(configuration);
        //PerformanceInterceptor(),OptimisticLockerInterceptor()
        //添加分页功能
//        sqlSessionFactory.setPlugins(new Interceptor[]{
//                paginationInterceptor()
//        });
//        sqlSessionFactory.setGlobalConfig(globalConfiguration());
        return sqlSessionFactory.getObject();
    }
}

